package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.ShopingBean
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.json.JSONArray
import org.json.JSONObject
import retrofit2.Response

class ConfirmOrderModel(app: Application) : MiddleVm(app) {

    val isShowing = MutableLiveData<Boolean>()

    /**
     * 1余额支付
     * 2礼金券 -分享积分
     * 3支付宝支付
     * 4体验券
     */
    val payType = MutableLiveData<Int>(0)
    val payTypeTxt = MutableLiveData<String>("请选择支付方式")
    val userRet = MutableLiveData<JsonResult<UserInfo>>()
    val commitRet = MutableLiveData<JsonResult<Ret>>()

    fun userInfo() {
        hold({
            server.userinfo().apply {
                body()?.let {
                    if (it.isSuccess) {
                        StoreUtil.saveBean(getApplication(), it.data!!)
                    }
                }
            }
        }, success = {
            userRet.value = it
        })
    }

    fun commitOrderByPayPwdValid(
        it: String,
        num: String,
        bean: ShopingBean,
    ) {
//        var t_message = ""
        runBlocking {
            isShowing.value = true
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }

                emit(emm)
            }.map {
                var emm: JsonResult<Ret>? = null

                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.goods_buy_form(
                                "${bean.goods_id}",
                                num,
                                bean.price
                            )
                            emm = ret.body()
                        }
                    } else {
                        emm = JsonResult<Ret>().apply {
                            code = it.code
                            message = it.message
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO).onCompletion {
                isShowing.postValue(false)
            }.catch { cause ->
                if (cause != null) {
                    toast("网络错误")
                }
            }.collectLatest {
                commitRet.value = it
            }

        }

    }

}